package com.cloudspace.jindun.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.Organization;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.List;

/**
 * Created by zengxianhua on 16/12/24.
 */

public class ContactOrganizationAdapter extends BaseAdapter{
    private List<Organization> datas;

    public void setDatas(List<Organization> data){
        datas = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(datas)){
            return 0;
        }

        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(datas)){
            return null;
        }

        if (position < 0 || position > datas.size()){
            return null;
        }

        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.list_orgnazition, null);

            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.imageView);
            holder.name = (TextView) convertView.findViewById(R.id.text);

            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();
        Organization orig = datas.get(position);
        holder.name.setText(orig.organName);
        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.icon, orig.iconUrl, Configs.IMAGE_CORNER_RADIUS);

        return convertView;
    }

    static class ViewHolder{
        ImageView icon;
        TextView name;
    }
}
